package cn.tedu.serviceEdu;

import cn.tedu.serviceEdu.entity.EduSubject;
import cn.tedu.serviceEdu.entity.dto.EduSubjectDto;
import cn.tedu.serviceEdu.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class subjectTree {

    @Autowired
    private EduSubjectService subjectService;

    @Test
    public void getSubjectTree() {
        List<EduSubject> list = subjectService.list(null);

        // 2 处理数据 父子关系
        List<EduSubject> resultList = new ArrayList<>();
        Map<String,EduSubject> cacheMap = new HashMap<>();

        list.forEach( eduSubject -> {
            // 3.1 获得父分类
            EduSubject parenEduSubject = cacheMap.get(eduSubject.getParentId());
            // 3.2 如果没有父添加到resultList中，如果有父追加父内部
            if(parenEduSubject == null) {
                resultList.add(eduSubject);
            } else {
                parenEduSubject.getChildren().add(eduSubject);
            }

            // 3.3 缓存自己
            cacheMap.put(eduSubject.getId() , eduSubject);

        });
        System.out.println(resultList);
    }
}
